
var OpenLayer = function(x1, y1, x2, y2,
						 mapId, projection, displayProjection,
						 titleCreate, url, version, featureType, featureNS, srsName, maxFeatures,
						 panelDisplayClass,
						 titleDraw, drawDisplayClass,
						 titleModify, modifyDisplayClass,
						 titleDelete,
						 titleSave, saveDisplayClass,
						 proxyHost){
	console.log('OpenLayer>>constructor');
	// *********** metodos ***********
	
	this.setProxyHost = function(url){
		OpenLayers.ProxyHost = url;
		console.log('setProxyHost: ' + url);
	};
	
	this.createExtend = function(x1, y1, x2, y2){
	    return new OpenLayers.Bounds(x1, y1, x2,y2);
	};
	
	this.createMap = function(mapId, projection, displayProjection, extend){
		console.log('createMap >> mapId: ' + mapId + ' projection: ' + projection + ' displayProjection: ' + displayProjection + ' extend: ' +extend);
		return new OpenLayers.Map(mapId, {
	        projection: new OpenLayers.Projection(projection),
	        displayProjection: new OpenLayers.Projection(displayProjection),
	        restrictedExtent: extend,
	        controls: [
	            new OpenLayers.Control.PanZoom(),
	            new OpenLayers.Control.Navigation()
	        ]
	    });
	};

	this.createOSM = function(){
		return new OpenLayers.Layer.OSM();
		
	};
	
	this.createSaveStrategy = function(){
		 return new OpenLayers.Strategy.Save();
	};
	
	this.createWfs = function(title, pUrl, version, pFeatureType, pFeatureNS, pSrsName, pMaxFeatures, saveStrategy){
		console.log('createWfs');
		console.log(title, pUrl, version, pFeatureType, pFeatureNS, pSrsName, pMaxFeatures);
		return new OpenLayers.Layer.Vector(title, {
	        projection:"EPSG:900913",    
	        strategies: [new OpenLayers.Strategy.BBOX(), saveStrategy],    
	        protocol: new OpenLayers.Protocol.WFS({
	            url: pUrl,
	            version: version,
	            featureType: pFeatureType,
	            featureNS: pFeatureNS,
	            srsName: pSrsName,
	            maxFeatures: pMaxFeatures
	        })
	        
	    });
	};
	
	//this.map = this.createMap(mapId, projection, displayProjection, this.extend);
	
	this.addLayers = function(){
		this.map.addLayers([this.osm, this.wfs]);
		};
	
	this.createPanel = function(pDisplayClass){
		return new OpenLayers.Control.Panel({
	        displayClass: pDisplayClass,
	        allowDepress: true
	    });
	};
	
	this.createDrawFeature = function(pTitle, pDisplayClass){
		console.log('createDrawFeature >> pTilte: ' + pTitle + 'pDisplayClass: ' + pDisplayClass);
		return  new OpenLayers.Control.DrawFeature(
		        this.wfs, OpenLayers.Handler.Polygon,
		        {
		            title: pTitle,
		            displayClass: pDisplayClass,
		            multi: true
		        }
		    );		
	};
	
	this.createModifyFeature = function(pTitle, pDisplayClass){
		return  new OpenLayers.Control.ModifyFeature(this.wfs, {
	        title: pTitle,
	        displayClass: pDisplayClass
	    });
	};
	
	this.createDeleteFeature = OpenLayers.Class(OpenLayers.Control, {
		    initialize: function(layer, options) {
		        OpenLayers.Control.prototype.initialize.apply(this, [options]);
		        this.layer = layer;
		        this.handler = new OpenLayers.Handler.Feature(
		            this, layer, {click: this.clickFeature}
		        );
		    },
		    clickFeature: function(feature) {
		        // if feature doesn't have a fid, destroy it
		        if(feature.fid == undefined) {
		            this.layer.destroyFeatures([feature]);
		        } else {
		            feature.state = OpenLayers.State.DELETE;
		            this.layer.events.triggerEvent("afterfeaturemodified", 
		                                           {feature: feature});
		            feature.renderIntent = "select";
		            this.layer.drawFeature(feature);
		        }
		    },
		    setMap: function(map) {
		        this.handler.setMap(map);
		        OpenLayers.Control.prototype.setMap.apply(this, arguments);
		    },
		    CLASS_NAME: "OpenLayers.Control.DeleteFeature"
		});
		
	this.createSaveFeature = function(pTitle, pDisplayClass){
		var saveFeature = new OpenLayers.Control.Button({
	        title: pTitle,
	        trigger: function() {
	            if(this.modifyFeature.feature) {
	                //edit.selectControl.unselectAll();
	            	this.modifyFeature.unselectFeature();
	            }
	            this.saveStrategy.save();
	        },
	        displayClass: pDisplayClass
	    });
		
		

	    return saveFeature;
	};
	
	// *********** atributos ***********
	
	this.proxyHost = this.setProxyHost(proxyHost);
	
	this.extend = this.createExtend(x1, y1, x2, y2);

	this.map = this.createMap(mapId, projection, displayProjection, this.extend);
	
	this.osm = this.createOSM();
	
	this.saveStrategy = this.createSaveStrategy();

	this.wfs = this.createWfs(titleCreate, url, version, featureType, featureNS, srsName, maxFeatures, this.saveStrategy);
		
	this.addLayers();
	
	// Panel con componentes
	this.panel = this.createPanel(panelDisplayClass);
	
	this.drawfeature = this.createDrawFeature(titleDraw, drawDisplayClass);
	
	this.modifyFeature = this.createModifyFeature(titleModify, modifyDisplayClass);
	
	this.deleteFeature = new this.createDeleteFeature(this.wfs, {title: titleDelete});
	
	this.saveFeature = this.createSaveFeature(titleSave, saveDisplayClass);
	
	this.panel.addControls([this.saveFeature, this.deleteFeature, this.modifyFeature, this.drawfeature]);

	this.map.addControl(this.panel);

    this.map.zoomToExtent(this.extend, true);
    
	
};

